package com.atguigu.controller;

import com.atguigu.client.SearchFeignClient;
import com.atguigu.constant.MqConst;
import com.atguigu.entity.ProductImage;
import com.atguigu.entity.ProductSalePropertyKey;
import com.atguigu.entity.ProductSpu;
import com.atguigu.entity.SkuInfo;
import com.atguigu.result.RetVal;
import com.atguigu.service.ProductImageService;
import com.atguigu.service.ProductSalePropertyKeyService;
import com.atguigu.service.SkuInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/product")
public class SkuController {

    @Autowired
    ProductSalePropertyKeyService productSalePropertyKeyService;

    @Autowired
    ProductImageService productImageService;

    @Autowired
    SkuInfoService skuInfoServices;
    @Autowired
    SearchFeignClient searchFeignClient;
    @Autowired
    RabbitTemplate rabbitTemplate;

    //http://127.0.0.1/product/querySalePropertyByProductId/12
    //根据spuId查询销售属性sku
    @GetMapping("querySalePropertyByProductId/{productId}")
    public RetVal querySalePropertyByProductId(@PathVariable Long productId){

        List<ProductSalePropertyKey> list=productSalePropertyKeyService.querySalePropertyByProductId(productId);
        return RetVal.ok(list);
    }

    //http://127.0.0.1/product/queryProductImageByProductId/12
    //根据商品spu的id查询销售属性sku图片
    @GetMapping("queryProductImageByProductId/{productId}")
    public RetVal queryProductImageByProductId(@PathVariable Long productId){

        QueryWrapper<ProductImage> wrapper = new QueryWrapper<>();
        wrapper.eq("product_id",productId);
        List<ProductImage> list = productImageService.list(wrapper);
        return RetVal.ok(list);
    }

    //http://127.0.0.1/product/saveSkuInfo
    //添加sku属性
    @PostMapping("saveSkuInfo")
    public RetVal saveProductSpu(@RequestBody SkuInfo skuInfo){

        skuInfoServices.saveProductSpu(skuInfo);
        return RetVal.ok();
    }


    //http://127.0.0.1/product/querySkuInfoByPage/1/10
    //sku列表分页查询
    @GetMapping("querySkuInfoByPage/{currentPageName}/{pageSize}")
    public RetVal querySkuInfoByPage(@PathVariable Long currentPageName,@PathVariable Long pageSize){
        Page<SkuInfo> skuInfoPage = new Page<>(currentPageName,pageSize);

        QueryWrapper<SkuInfo> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id");
        skuInfoServices.page(skuInfoPage,wrapper);
        return RetVal.ok(skuInfoPage);
    }


    //http://127.0.0.1/product/onSale/38
    //上架
    @GetMapping("onSale/{skuId}")
    public RetVal onSale(@PathVariable Long skuId){
        SkuInfo skuInfo = new SkuInfo();
        skuInfo.setId(skuId);
        skuInfo.setIsSale(1);
        skuInfoServices.updateById(skuInfo);
//        //远程调用shop-search
//        searchFeignClient.onSale(skuId);
        //发送消息(交换机+队列+消息)
        rabbitTemplate.convertAndSend(MqConst.ON_OFF_SALE_EXCHANGE,MqConst.ON_SALE_ROUTING_KEY,skuId);
        return RetVal.ok();
    }

    //http://127.0.0.1/product/offSale/37
    //下架
    @GetMapping("offSale/{skuId}")
    public RetVal offSale(@PathVariable Long skuId){
        SkuInfo skuInfo = new SkuInfo();
        skuInfo.setId(skuId);
        skuInfo.setIsSale(0);
        skuInfoServices.updateById(skuInfo);
//        //远程调用shop-search
//        searchFeignClient.offSale(skuId);
        rabbitTemplate.convertAndSend(MqConst.ON_OFF_SALE_EXCHANGE,MqConst.OFF_SALE_ROUTING_KEY,skuId);
        return RetVal.ok();
    }

}
